package com.hsd.znsh.code.sms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsd.znsh.code.ValidateCode;
import com.hsd.znsh.code.iml.AbstractValidateCodeProcessor;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.properties.SecurityConstants;
import com.hsd.znsh.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;


@Component("smsValidateCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

	/**
	 * 短信验证码发送器
	 */
	@Autowired
	private SmsCodeSender smsCodeSender;

	@Autowired
	private ObjectMapper objectMapper;
	
	@Override
	protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
		String paramName = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;
		String mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
		smsCodeSender.send(mobile, validateCode.getCode());
		request.getResponse().setContentType("application/json;charset=UTF-8");
		request.getResponse().getWriter().write(objectMapper.writeValueAsString(ResultVOUtil.success()));
	}

}
